package com.warm.mybatis.service.impl;
import java.util.Date;
import java.util.Random;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.warm.mybatis.dao.BaseUserDao;
import com.warm.mybatis.entity.BaseUserEntity;
import com.warm.mybatis.service.BaseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
public class BaseUserServiceImpl extends ServiceImpl<BaseUserDao, BaseUserEntity> implements BaseUserService {

    @Resource
    BaseUserDao baseUserDao;

    @Override
    public void insertUserNoTr() {
        insertUser();
    }

    @Override
    public void insertUserNoTrThrow() {
        insertUser();
        throw new RuntimeException();
    }

    @Transactional
    @Override
    public void insertUserTr() {
        insertUser();
    }

    @Transactional
    @Override
    public void insertUserTrThrow() {
        insertUser();
        throw new RuntimeException();
    }

    @Override
    public void insertUserTrCatch() {
        try {
            insertUser();
            throw new RuntimeException();
        } catch (RuntimeException e){

        }
    }

    private void insertUser(){
        BaseUserEntity user = new BaseUserEntity();
        int i = new Random().nextInt(10);
        user.setId("a" + i);
        user.setAccount("aa" + i);
        user.setPassword("aa" + i);
        user.setName("aa" + i);
        user.setSex(10);
        user.setAge(20);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        baseUserDao.insert(user);
    }
}
